<!DOCTYPE html>
<html>
<head>
	<title>表单验证</title>
	<script type="text/javascript" src="./from-vail.js"></script>
</head>
<body>
	<form id="myForm"  method="post">
		用户名：<input type="text" name="myName">
		密码：<input type="password" name="myPassword">
		电话号码：<input type="text" name="myPhoto">
		<button>提交</button>
	</form>

</body>
	<script type="text/javascript">

		var myForm=document.getElementById('myForm');
		strategies['minLength']=function(value,length,errorMsg){
				if(value.length<parseInt(length)){
					return errorMsg;
				}
			}
		
		function vaildatorFun(){

			var vaildator=new Vaildator();

			vaildator.add(myForm.myName,[{
				strategy:"isNoEmpty",
				errorMsg:"用户名不能为空"
			},{
				strategy:"minLength:3",
				errorMsg:"用户名长度不小于3"
			}]);
			vaildator.add(myForm.myPassword,[{
				strategy:'minLength:10',
				errorMsg:"密码长度要大于10位"
			}]);
			vaildator.add(myForm.myPhoto,[{
				strategy:'reg:/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$/',
				errorMsg:"手机号格式不正确"
			}]);
			var errorMsg=vaildator.start();
			return errorMsg;
		}

		myForm.onsubmit=function(){
			var errorMsg=vaildatorFun();
			if(errorMsg){
				alert(errorMsg);
			}else{
				alert("验证通过")
			}
			return false
		}
	</script>

</html>